define(['../../carrys/cityList','../../carrys/jsonList','../../../../js/lib/other/sweet-alert'],function(cityData,jsonList) {
    return ['$scope','$interval','$timeout','$routeParams','$route','bizProxy', function($scope,$interval,$timeout,$routeParams,$route,bizProxy) {
    	$scope.$emit("top-show",'true');//顶部显示
    	$scope.$emit('menuTypeLight','case_src_lib'); 	//控制底部menu高亮
    	$scope.$emit('bub-menu','true'); 	//隐藏底部menu
    	$scope.$emit('flag-tag','1'); 	//显示底部文字
    	
    	$scope.htmlStru = '#case_src_lib .wrap_box'; //控制刷新
    	var refresh = '0';//"0":刷新整个案源库列表;"1":刷新精准案源列表
    	$scope.caseLength = 0; //默认案源条数
    	var totalPageNum = 1;var pageFlag = 1;
    	$scope.caseTypeList = jsonList.caseTypeData;
    	
    	
    	//监听刷新状态
    	$scope.$on('caselibRefresh', function(event, data) {
        	if(data){
        		// if(refresh === '0'){
                $timeout(function(){
                    pageFlag = 1;
                    getProxyData(false);
                },0); 
                   
        		// }else if(refresh === '1'){
        			// getCaseListBytype(false); 
        		// }
        		
        	}
        });
    	
    	
    	//$routeParams.flag:"1":显示案件 、"2": 显示发布
    	if($routeParams.flag != "" && $routeParams.flag != null){
    		if($routeParams.flag === "1"){
    			//默认显示
        		$timeout(function(){
        			$scope.isStateChange = false;
                	$scope.isCase_issue = false;
        			getProxyData(false);
        		},0)
    		}else if($routeParams.flag === "2"){
    			if(!(resultData.loginUserData.userName != "" && resultData.loginUserData.userName != null)){
    	    		//未完善资料用户不可申请免息贷
    				FIREFLY("请先去'我的'里面完善个人信息", "","",function(){
    					$timeout(function(){
    						history.back();
    					},0);
    				});
    				return;
    			}
    			$scope.$emit('bub-menu','true'); 	//隐藏底部menu
    			$scope.isStateChange = true;
            	$scope.isCase_issue = true;
            	$scope.userName = resultData.loginUserData;
    		}
    	}else{
    		//默认显示
    		$timeout(function(){
    			$scope.isStateChange = false;
            	$scope.isCase_issue = false;
    			getProxyData(false);
    		},0)
    	}
    	
    	//发布案件信息
    	$scope.caseTitle = "",$scope.caseOutline = "", $scope.telNo = transData.mobileNo; 
    	
    	//精准搜索控制
    	$scope.isSlide_up = false,$scope.isSlide_down = false,$scope.proWord = "展开更多";
    	$scope.debtorType = "",$scope.debtorStatus = "",$scope.limitArea = "";
    	
    	//整理省市数据
    	var provinceList = new Array();
    	var cityListTemp = new Array();
    	var districtTemp = new Array();
    	$scope.keyword = "",$scope.provinceName = "",$scope.cityName ="",$scope.districtName = "";
    	$scope.provinceName1 = "",$scope.cityName1 ="",$scope.districtName1 = "";
    	$scope.provinceName2 = "",$scope.cityName2 ="",$scope.districtName2 = "";
    	$scope.provinceName3 = "",$scope.cityName3 ="",$scope.districtName3 = "";
    	collateData();//整理列表数据
    	
    	/*//点击显示案件列表
    	$scope.showCaseList = function(){
    		$scope.$emit('bub-menu','false'); 	//显示底部menu
    		$timeout(function(){
    			$scope.isStateChange = false;
            	$scope.isCase_issue = false;
            	if(! $scope.tempCaseList){
            		getProxyData(true);
            	}
    		},0)
    	}*/
    	
    	//点击显示发布案件
    	$scope.showPubCase = function(){
    		if(transData.sessionId != "" && transData.mobileNo != "" && resultData.loginUserData != ""){
    			if(resultData.loginUserData.userName != "" && resultData.loginUserData.userName != null){
    				//$scope.$emit('bub-menu','true'); 	//隐藏底部menu
            		//$scope.isStateChange = true;
                	//$scope.isCase_issue = true;
                	//$scope.telNumber = transData.mobileNo;
                	//$scope.userName = resultData.loginUserData;
                    $scope.$emit("goNext","/applyRisk");
    			}else{//未完善资料用户不可发布
					$scope.$emit("promptAlertMsg","请先去'我的'里面完善个人信息");
	    			$scope.$emit("showCollect",true);
	    			$timeout(function(){
	    				$scope.$emit("showCollect",false);
	    			},transData.promptTime);
    			}
    			
    		}else{
    			transData.gotoUrl = "/caseSrcLib";
    			$scope.$emit("goNext","/login");
    		}
    	}
    	
    	//点击显示精准搜索
    	$scope.showSearch = function(){
    		$timeout(function(){
    			$scope.isSlide_up = true;
    			$scope.limitAreaData = jsonList.limitAreaList;
    		},0);
    	}
    	//点击关闭精准搜索
    	$scope.closeSearch = function(){
    		$timeout(function(){
    			$scope.isSlide_up = false;
    		},0);
    	}
    	
    	//点击显示更多（精准搜索）
    	$scope.showMore = function(){
    		if($scope.isSlide_down){
    			$scope.isSlide_down = false;
    			$scope.proWord = "展开更多"
    		}else{
    			$scope.isSlide_down = true;
    			$scope.proWord = "关闭更多"
    			$scope.debtorTypeData = jsonList.debtorTypeList;
    			$scope.debtorStatusData = jsonList.debtorStatusList;
    		}
    	}

    	//点击关闭案件类型
//    	$scope.closeType = function(){
//    		$scope.issCaselide_up = false;
//    	}
    	//点击显示案件类型
//    	$scope.selectCaseType = function(){
//    		$scope.issCaselide_up = true;
//    		if($scope.caseType !=""){
//				for(var j=0;j<caseTypeList.length;j++){
//					if($scope.caseType == caseTypeList[j].name){
//						caseTypeList[j].sel = true;
//					}else{
//						caseTypeList[j].sel = false;
//					}
//				}
//    			$scope.caseTypeList = caseTypeList;
//    		}else{
//    			for(var j=0;j<caseTypeList.length;j++){
//					caseTypeList[j].sel = false;
//				}
//    			$scope.caseTypeList = caseTypeList;
//    		}
//    		
//    	}
    	
    	//选择案件类型
//    	$scope.selectType = function(flag){
//    		caseTypeList.forEach(function(e){
//    			e.sel = false;
//    		});
//    		caseTypeList[flag].sel = true;
//    	}
    	
    	//确认选择
//    	$scope.confirmBtn = function(){
//    		$scope.issCaselide_up = false;
//    		for(var j=0;j<caseTypeList.length;j++){
//				if(caseTypeList[j].sel){
//					$scope.caseType = caseTypeList[j].name;
//					break;
//				}
//			}
//    	}
    	
    	//点击提交发布
    	/*$scope.subIssue = function(){
//    		if(! $scope.caseTitle){
//    			$scope.$emit("promptAlertMsg","案件标题不能为空");
//    			$scope.$emit("showCollect",true);
//    			$timeout(function(){
//    				$scope.$emit("showCollect",false);
//    			},transData.promptTime);
//    			return;
//    		}else 
    			
    		if(! $scope.plaintiffName){
    			$scope.$emit("promptAlertMsg","原告人姓名不能为空");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
    			return;
    		}else if(! $scope.defendantName){
    			$scope.$emit("promptAlertMsg","被告人姓名不能为空");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
    			return;
    		}else if(! $scope.caseType){
    			$scope.$emit("promptAlertMsg","案件类型不能为空");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
    			return;
    		}else if(! $scope.caseDescribe){
    			$scope.$emit("promptAlertMsg","案情描述不能为空");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
    			return;
    		}else if(isNaN($scope.caseAmt)){
    			$scope.$emit("promptAlertMsg","涉案金额格式不正确");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
    			return;
    		}else if(! $scope.provinceName3){
    			$scope.$emit("promptAlertMsg","所在区域省不能为空");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
    			return;
    		}else if(! $scope.cityName3){
    			$scope.$emit("promptAlertMsg","所在区域市不能为空");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
    			return;
    		}else if(! $scope.districtName3){
    			$scope.$emit("promptAlertMsg","所在区域县不能为空");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
    			return;
    		}else if(! $scope.telNumber){
    			$scope.$emit("promptAlertMsg","联系电话不能为空");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
    			return;
    		}else{
    			issueCase();
    		}
    		
    	}*/
    	
    	//清空搜索
    	$scope.clearSearch = function(){
    		var sweetData = {
					title : "",
					text : "确认要清空所有选项吗？",
					type : "",
					showCancelButton : true,
					closeOnConfirm : false
				};
    		FIREFLY(sweetData,function(){
				$timeout(function(){
					$scope.debtorType = "",$scope.debtorStatus = "",$scope.limitArea = "",
					$scope.keyword = "",$scope.provinceName = "",$scope.cityName ="",$scope.districtName = "",
			    	$scope.provinceName1 = "",$scope.cityName1 ="",$scope.districtName1 = "",
			    	$scope.provinceName2 = "",$scope.cityName2 ="",$scope.districtName2 = "";
				},0);
			},function(){return;});
    	}
    	
    	//确认搜索
    	$scope.confirmSearch = function(){
    		// getCaseListBytype(true);
            $scope.isSlide_up = false;
            pageFlag = '1' 
            getProxyData(false);
    	}
    	
    	//点击显示案件概况
    	$scope.goCaseProfile = function(id){
    		if(id != "" && id != null){
                if(transData.sessionId != null && transData.sessionId != ""){
                    // caseDetailInfo(id);
                    //判断是不是律师
                    if(transData.userType === '1'){
                        caseDetailInfo(id);
                    }else{
                        FIREFLY("对不起，您还不是律师用户", "请先在'个人信息'中升级为律师用户！","",function(){	});
                    }
                }else{
                    $scope.$emit("goNext","/login");
                }
    		}
    	}
    	
    	
    	//显示省份
    	$scope.provinceData = provinceList;
    	//显示城市
    	$scope.clickCity = function(data,flag){
             pageFlag = '1' 
             getProxyData(false);
    		// var cityList = new Array();
    		// var provinceName = data.province;
    		// for(var i=0;i<cityListTemp.length;i++){
    		// 	if(cityListTemp[i].province == provinceName){
    		// 		var tempData = {'city': cityListTemp[i].city};
    		// 		cityList.push(tempData);
    		// 	}
    		// }
    		// if(flag === '0'){
    		// 	$scope.cityData = cityList;
    		// }else if(flag === '1'){
    		// 	$scope.cityData1 = cityList;
    		// }else if(flag === '2'){
    		// 	$scope.cityData2 = cityList;
    		// }else if(flag === '3'){
    		// 	$scope.cityData3 = cityList;
    		// }
    		
    	}
    	//显示地区
    	// $scope.clickArea = function(data,flag) {
    	// 	var districtList = new Array();
    	// 	var districtList1 = new Array();
    	// 	var cityName = data.city;
    	// 	for(var i=0;i<districtTemp.length;i++){
    	// 		if(districtTemp[i].city == cityName){
    	// 			districtList = districtTemp[i].district;
    	// 			break;
    	// 		}
    	// 	}
    	// 	for(var i=0;i< districtList.length;i++){
    	// 		var tempData = {'district':districtList[i]};
    	// 		districtList1.push(tempData);
    	// 	}
    	// 	if(flag === '0'){
    	// 		$scope.districtData = districtList1;
    	// 	}else if(flag === '1'){
    	// 		$scope.districtData1 = districtList1;
    	// 	}else if(flag === '2'){
    	// 		$scope.districtData2 = districtList1;
    	// 	}else if(flag === '3'){
    	// 		$scope.districtData3 = districtList1;
    	// 	}
    		
    	// }
    	
    	//发布案源
/*    	function issueCase(){
    		var headerInfo = {},bizInfo = {};
        	headerInfo.pageNum = '';
        	headerInfo.pageSize = '';
        	headerInfo.sessionId = transData.sessionId;
        	
        	bizInfo.caseTopic = $scope.plaintiffName + "诉" + $scope.defendantName + $scope.caseType + "一案";
        	bizInfo.caseDesc = $scope.caseDescribe;
        	bizInfo.contactMobile = $scope.telNumber;
        	bizInfo.caseAmount = $scope.caseAmt;
        	bizInfo.creditorName = $scope.plaintiffName;
        	bizInfo.debtorName = $scope.defendantName;
        	bizInfo.caseMemo = $scope.caseExplain;
        	bizInfo.caseType = $scope.caseType;
        	if(($scope.provinceName3).province == "北京" || ($scope.provinceName3).province  == "上海" || ($scope.provinceName3).province  == "天津" || ($scope.provinceName3).province == "重庆"){
        		bizInfo.caseProvince = ($scope.cityName3).city;
        		bizInfo.caseCity = ($scope.districtName3).district;
        	}else{
        		bizInfo.caseProvince = ($scope.provinceName3).province;
        		bizInfo.caseCity = ($scope.cityName3).city;
        	}
    		$scope.$emit("loadingChange",true);
    		bizProxy.queryProxyBizData("caseRelease",headerInfo,bizInfo,
    				function(tempData){//SUCCESS
		    			$scope.$emit("loadingChange",false);
						$timeout(function(){
							$scope.$emit("goNext","/sucCaseSub")
						},0);
		    		},function(data){
		    			if(data != ""){//FAIL
		    				$scope.$emit("loadingChange",false);
							if(data.errorCode == ERROR_CODE.noLogin){
								FIREFLY("", data.errorMsg,"",function(){
										//调用退出登录
										transData.mobileNo = "";
		    							transData.password = "";
		    							transData.sessionId = "";
		    							transData.userType = "";
		    							resultData.loginUserData = "";
		    							resultData.loginResultData = "";
		    							$timeout(function(){
		    								$scope.$emit("goNext","/mine");
		    							},0);
								});
							}else{
								$scope.$emit("loadingChange",false);
								$scope.$emit("promptAlertMsg",data.errorMsg);
								$scope.$emit("showCollect",true);
								$timeout(function(){
									$scope.$emit("showCollect",false);
								},transData.promptTime);
							}
		    			}else{
		    				$scope.$emit("loadingChange",false);
							$scope.$emit("promptAlertMsg","连接超时");
							$scope.$emit("showCollect",true);
							$timeout(function(){
								$scope.$emit("showCollect",false);
							},transData.promptTime);
		    			}
		    		});
    	}*/
    	
    	//根据条件查询安源列表
    	function getCaseListBytype(flag){
            $scope.tempCaseList = new Array();
    		//将滚动页数置为1
    		pageFlag = 1;
    		
    		var headerInfo = {},bizInfo = {};
        	headerInfo.pageNum = '1';
        	headerInfo.pageSize = '1000';
        	headerInfo.sessionId = "";
        	
        	// bizInfo.caseTopicKeyText = $scope.keyword;
        	// bizInfo.courtProvince = ($scope.provinceName != "" && $scope.provinceName != null)?($scope.provinceName).province : "";
        	// bizInfo.courtCity = ($scope.cityName != "" && $scope.cityName != null)?($scope.cityName).city : "";
        	// bizInfo.courtCounty = ($scope.districtName != "" && $scope.districtName != null)?($scope.districtName).district : "";
        	// bizInfo.caseAmount = ($scope.limitArea != "" && $scope.limitArea != null)?($scope.limitArea).type : "";
        	// bizInfo.debtorProvince = ($scope.provinceName1 != "" && $scope.provinceName1 != null)?($scope.provinceName1).province : "";
        	// bizInfo.debtorCity = ($scope.cityName1 != "" && $scope.cityName1 != null)?($scope.cityName1).city : "";
        	// bizInfo.debtorCounty = ($scope.districtName1 != "" && $scope.districtName1 != null)?($scope.districtName1).district : "";
        	// bizInfo.assetProvince = ($scope.provinceName2 != "" && $scope.provinceName2 != null)?($scope.provinceName2).province : "";
        	// bizInfo.assetCity = ($scope.cityName2 != "" && $scope.cityName2 != null)?($scope.cityName2).city : "";
        	// bizInfo.assetCounty = ($scope.districtName2 != "" && $scope.districtName2 != "")?($scope.districtName2).district : "";
        	// bizInfo.debtor_type = ($scope.debtorType != "" && $scope.debtorType != null)?($scope.debtorType).name : "";
        	// bizInfo.caseStatus = ($scope.debtorStatus != "" && $scope.debtorStatus != null)?($scope.debtorStatus).name : "";
    		
            if(!$scope.provinceName){
                bizInfo.province = "";
            }else{
                bizInfo.province = ($scope.provinceName).province;
            }

            if(!$scope.cityName){
                bizInfo.city = "";
            }else{
                bizInfo.city = ($scope.cityName).city;
            }
            
            console.log("bizInfo.province",bizInfo.province)
            console.log("bizInfo.city",bizInfo.city)
    		$scope.$emit("loadingChange",flag);
    		bizProxy.queryProxyBizData('matchCaseList',headerInfo,bizInfo,
    				function(tempData){//SUCCESS
		    			var tempCaseList = JSON.parse(tempData.body).caseInfoList;
						$timeout(function(){
							refresh = '1';
							$scope.caseLength = tempCaseList.length; //案源件数
							$scope.tempCaseList = tempCaseList;
							$scope.$emit("loadingChange",false);
							$scope.isSlide_up = false;
						},0);
		    		},function(data){
		    			if(data != ""){//FAIL
		    				$scope.$emit("loadingChange",false);
							if(data.errorCode == ERROR_CODE.noLogin){
								FIREFLY("", data.errorMsg,"",function(){
										//调用退出登录
										transData.mobileNo = "";
		    							transData.password = "";
		    							transData.sessionId = "";
		    							transData.userType = "";
		    							resultData.loginUserData = "";
		    							resultData.loginResultData = "";
		    							$timeout(function(){
		    								$scope.$emit("goNext","/mine");
		    							},0);
								});
							}else{
								$scope.$emit("loadingChange",false);
								$scope.$emit("promptAlertMsg",data.errorMsg);
								$scope.$emit("showCollect",true);
								$timeout(function(){
									$scope.$emit("showCollect",false);
								},transData.promptTime);
							}
		    			}else{
		    				$scope.$emit("loadingChange",false);
							$scope.$emit("promptAlertMsg","连接超时");
							$scope.$emit("showCollect",true);
							$timeout(function(){
								$scope.$emit("showCollect",false);
							},transData.promptTime);
		    			}
		    		});
    	}
    	
    	 $scope.ele = document.querySelector('#case_src_lib .wrap .case_src_list .wrap_box > ul');
    	var onGetProxyData = false;
 		$scope.loadMore = function() {
 			console.log("allGoods-----loadMore");
 			if(refresh === '1'){
    			return;
    		}
 			
 			if(pageFlag <= totalPageNum){
 				if(onGetProxyData){return;};
 				getProxyData(false);
         	}
         };
    	
    	
    	
    	//获取代理接口数据(案件列表)
    	function getProxyData(flag){
            onGetProxyData=true;
        	var headerInfo = {},bizInfo = {};
        	headerInfo.pageNum = pageFlag + '';
	       	headerInfo.pageSize = '20';
        	headerInfo.sessionId = transData.sessionId;

        	if(!$scope.provinceName){
                bizInfo.province = "";
            }else{
                bizInfo.province = ($scope.provinceName).province;
            }

            if(!$scope.cityName){
                bizInfo.city = "";
            }else{
                bizInfo.city = ($scope.cityName).city;
            }
    		$scope.$emit("loadingChange",flag);

            console.log(bizInfo,'++',headerInfo)
    		bizProxy.queryProxyBizData("matchCaseList",headerInfo,bizInfo,
    				function(tempData){//SUCCESS
    					$scope.$emit("loadingChange",false);
		    			if(tempData.body != "" && tempData.body != null && tempData.body != "[]"){
							var tempCaseList = JSON.parse(tempData.body).caseInfoList;
							totalPageNum = tempData.totalPage;
							console.log("案件列表=",JSON.parse(tempData.body))
							$timeout(function(){
								$scope.caseLength = tempData.totalNum *1; //案源件数
                                if(pageFlag==1){
                                    $scope.tempCaseList = new Array();
                                }
								tempCaseList.forEach(function(e){
				   	    			$scope.tempCaseList.push(e);
			   					});
//								$scope.tempCaseList = tempCaseList;
                                pageFlag++;
                                onGetProxyData=false;
							},0);
						}
//		    			else{
//							$timeout(function(){
//								$scope.caseLength = 0; //案源件数
//								$scope.tempCaseList = "";
//								$scope.$emit("loadingChange",false);
//							},0);
//						}
		    		},function(data){
    					console.log(data);
                    	onGetProxyData = false;
		    			if(data != ""){//FAIL
		    				$scope.$emit("loadingChange",false);
							if(data.errorCode == ERROR_CODE.noLogin){
								FIREFLY("", data.errorMsg,"",function(){
										//调用退出登录
										transData.mobileNo = "";
		    							transData.password = "";
		    							transData.sessionId = "";
		    							transData.userType = "";
		    							resultData.loginUserData = "";
		    							resultData.loginResultData = "";
		    							$timeout(function(){
		    								$scope.$emit("goNext","/mine");
		    							},0);
								});
							}else{
								$scope.$emit("promptAlertMsg",data.errorMsg);
								$scope.$emit("showCollect",true);
								$timeout(function(){
									$scope.$emit("showCollect",false);
								},transData.promptTime);
							}
		    			}else{
		    				$scope.$emit("loadingChange",false);
		    				$scope.$emit("promptAlertMsg","连接超时");
							$scope.$emit("showCollect",true);
							$timeout(function(){
								$scope.$emit("showCollect",false);
							},transData.promptTime);
		    			}
		    		});
    	}
    	
    	//案源详细信息
    	function caseDetailInfo(id){
    		$scope.$emit("loadingChange",true);
    		var headerInfo = {},bizInfo = {};
        	headerInfo.pageNum =  '';
	       	headerInfo.pageSize = '';
        	headerInfo.sessionId = transData.sessionId;
        	
    		bizInfo.caseId = id;
    		bizProxy.queryProxyBizData("queryCaseInfoDetail",headerInfo,bizInfo,
    				function(tempData){
    			console.log(JSON.parse(tempData.body))
		    			$timeout(function(){
		    				$scope.$emit('loadingChange', false);
		    				resultData.caseDetailData = "";
    						resultData.caseDetailData = JSON.parse(tempData.body);
							$scope.$emit('goNext',"/caseProfile");
						},0);
		    		},function(data){
		    			$scope.$emit('loadingChange', false);
		    			$scope.$emit("promptAlertMsg",data);
		    			$scope.$emit("showCollect",true);
		    			$timeout(function(){
		    				$scope.$emit("showCollect",false);
		    			},transData.promptTime);
		    		});
    		
    	}
    	
    	//整理省份数据
    	function collateData() {
    		cityListTemp = new Array();
    		districtTemp = new Array();
			for(var key in cityData.addressDetail){
				var tempData = {"province":key};
				provinceList.push(tempData);
				collateCity(key,cityData.addressDetail[key]);
			}
    	}
    	//整理城市数据
		function collateCity(data1,data2) {
			for(var key in data2){
				var tempList = {'province':data1,'city': key};
				cityListTemp.push(tempList);
				districtCity(key,data2[key]);
			}
		}
		//整理县区数据
		function districtCity(data1,data2){
			var tempList = {'city': data1,'district': data2};
			districtTemp.push(tempList);
		}
    	
        $scope.$apply();
    }];
});